package com.unknow.first.dapp.admin.account.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.lianziyou.chain.common.api.CommonPage;
import com.lianziyou.chain.common.api.CommonParam;
import com.unknow.first.dapp.admin.account.mapper.AddressTransfer;
import com.unknow.first.dapp.admin.account.service.AddressTransferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.cloud.annotation.SystemResource;
import org.cloud.constant.CoreConstant;
import org.cloud.vo.CommonApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/address/transfer")
@Slf4j
@SystemResource(path = "/admin/address/transfer")
@Api(value = "管理员：转账相关API", tags = "管理员：转账相关API")
public class AdminAddressTransferController {

    @Autowired
    private AddressTransferService addressTransferService;


    @SystemResource(value = "/admin-list", description = "管理员获取内部转账记录", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    @ApiOperation("管理员获取内部转账记录")
    public CommonApiResult<CommonPage<AddressTransfer>> list(Long id, Long coinId, String from, String to,
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, String sorts,
        CommonParam pageParam) {
        QueryWrapper<AddressTransfer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(id != null, AddressTransfer::getId, id).eq(coinId != null, AddressTransfer::getCoinId, coinId)
            .eq(!StringUtils.isEmpty(from), AddressTransfer::getFromAddress, from).eq(!StringUtils.isEmpty(to), AddressTransfer::getToAddress, to)
            .ge(startTime != null, AddressTransfer::getCreatedDate, startTime).lt(endTime != null, AddressTransfer::getCreatedDate, endTime);
        if (StringUtils.isEmpty(sorts)) {
            sorts = "id desc";
        }
        PageHelper.startPage(pageParam.getPage(), pageParam.getLimit(), sorts);
        List<AddressTransfer> transferList = addressTransferService.getBaseMapper().selectList(queryWrapper);
        CommonPage<AddressTransfer> page = CommonPage.restPage(transferList);
        return CommonApiResult.createSuccessResult(page);
    }

}
